<?php

namespace MLM\Model\Sysconfig;


use MLM\Model\Sysconfig;

class Storage extends \MLM\Model\Generic\Db\Storage {
    
    public function save(Sysconfig $sysconfig){

        $insert = $this->_sql->insert()
                ->into('sysconfig')
                ->values($sysconfig->pull());
        
        $result = $this->execute($insert);
        
        return $result;
    }

    public function edit($dataProduct, $where){

        $update = $this->_sql->update()
                ->table('sysconfig')
                ->set($dataProduct)
                ->where($where);

        $result = $this->execute($update);

        return $result;

    }


    public function delConfig($configId){

        $stmt = $this->_db->query("delete from sysconfig where sysconfig_id = $configId");
        return $stmt->execute() ;

    }

    
    
    public function getConfigList($configName, $start, $limit, $searchField = "") {

        $sql = "select count(1) as total
                from sysconfig ";


        if ($configName != ""){
            $sql .= " where sysconfig_name like '%$configName%' ";
        }

        if ($searchField != ""){
            $sql .= " where $searchField ";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute();

        while($getTotal->next()){
                $res = $getTotal->current();
                $rows = $res['total'];                
        }


        $data = array($rows, $total);


        $sql = "select * from sysconfig ";


        if ($configName != ""){
            $sql .= " where sysconfig_name like '%$configName%' ";
        }

        if ($searchField != ""){
            $sql .= " where $searchField ";
        }

        $sql .= " order by sysconfig_name ";

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }

        $stmt = $this->_db->query($sql);
        $result = $stmt->execute();

        $dataBonus = array();
        while($result->next()){
                $res = $result->current();
                
                $aksi = "<a href='javascript: void(0)' onclick='edit(".$res['sysconfig_id'].")'>Edit</a>";
                $aksi .= " | <a href='javascript: void(0)' onclick='del(".$res['sysconfig_id'].")'>Hapus</a>";

                array_push($dataBonus, array("NamaConfig" => $res['sysconfig_name'],
                                             "IsiAngka" => $res['int_value'],
                                             "IsiText" => $res['str_value'],
                                             "Aksi" => $aksi));
        }


        array_push($data, $dataBonus);

        return $data;
    }
	

	
    public function getConfigDetail($key){

        $sql = "select * from sysconfig where sysconfig_id = ? ";


        $stmt = $this->_db->query($sql);
        $res = $stmt->execute(array($key));
        return $res;
    }


     public function getConfigByName($key){

        $sql = "select *
                from sysconfig
                where sysconfig_name = ?";

        $stmt = $this->_db->query($sql);
        $res = $stmt->execute(array($key));

        return $res;
    }


    public function updateProduct($data, $where){

        $sql = $this->update()
                    ->table('members')
                    ->set($data)
                    ->where($where);

        $result = $this->execute($sql);

        return $result;
    }

    
}
